package serv

import (
	"d-serv-go/server/core/bean"
	"d-serv-go/server/core/cache"
	"d-serv-go/server/core/consts"
	"d-serv-go/server/core/db"
	strutils "d-serv-go/server/core/util/stringutils"
	"strings"
)

var servBean = NewServBean()

type ServBean struct {
	*bean.Bean
}

func (b *ServBean) GetPageCount(def int) int {
	return b.GetInt("SERV_PAGE_COUNT", def)
}

func (b *ServBean) GetAllItems() *bean.Bean {
	allItems := b.GetBean(consts.ITEMS)
	if allItems == nil {
		return bean.NewBean("")
	}
	return allItems
}

func (b *ServBean) GetPKey() string {
	return b.GetStr("SERV_KEYS")
}

func (b *ServBean) GetSqlOrder() string {
	return b.GetStr("SERV_SQL_ORDER")
}

func (b *ServBean) GetTableView() string {
	viewName := b.GetStr("TABLE_VIEW")
	return viewName
}

func (b *ServBean) GetServDefWhere() string {
	ctx := db.Ctx().Ctx
	servSqlWhere := strutils.NewStringBuilder()
	where := b.GetSqlWhere()
	if len(where) > 0 {
		if strings.Index(where, "@@") >= 0 { // 存在数据权限
			where = db.DataAclMgr.ReplaceDataAcl(where)
		}
		if strings.Index(where, "@") >= 0 { // 存在变量替换
			where = ServUtils.ReplaceSysVars(where)
		}
		servSqlWhere.Append(where)
	}
	if len(b.GetCmpy()) > 0 { // 自动处理存在公司字段的过滤
		// TODO
		cmpy := ctx.Locals(consts.CMPYCODE).(string)
		if len(cmpy) > 0 {
			servSqlWhere.Append(" and ").Append(b.GetCmpy()).Append("='").Append(cmpy).Append("'")
		}
	}
	return servSqlWhere.Append(b.GetServExpressionWhere()).ToString()
}

func (b *ServBean) GetCmpy() string {
	return b.GetStr("SERV_CMPY")
}

func (b *ServBean) GetSqlWhere() string {
	return b.GetStr("SERV_SQL_WHERE")
}

func (b *ServBean) GetServExpressionWhere() string {
	return ""
}

func (b *ServBean) GetViewItems() []*bean.Bean {
	var list = make([]*bean.Bean, 0)
	for _, b := range b.GetBeans(consts.ITEMS_VIEW) {
		list = append(list, b)
	}
	return list
}

func (b *ServBean) GetItem(key string) *bean.Bean {
	itemsBean := b.GetAllItems()
	return itemsBean.GetBean(key)
}

func (b *ServBean) GetDictCodes() string {
	return b.GetStr("SERV_DICT_CACHE")
}

func (b *ServBean) GetAct(act string) *bean.Bean {
	acts := b.GetBean(consts.ACTS)
	if acts != nil {
		return acts.GetBean(act)
	}
	return nil
}

func (b *ServBean) GetTableAction() string {
	return b.GetStr("TABLE_ACTION")
}

func (b *ServBean) GetTableItems() []*bean.Bean {
	return b.GetBeans(consts.ITEMS_TABLE)
}

func (b *ServBean) GetUniqueGroups() *bean.Bean {
	if b.Contains(consts.ITEM_UNIQUE_GROUP) {
		return b.GetBean(consts.ITEM_UNIQUE_GROUP)
	} else {
		return nil
	}
}

func (b *ServBean) HasFile() bool {
	return b.GetInt("SERV_FILE_FLAG") == consts.YES_INT
}

func (b *ServBean) GetSrcId() string {
	return b.GetStr("SERV_SRC_ID")
}

func (b *ServBean) HasLink() bool {
	return b.GetInt("SERV_LINK_FLAG") == consts.YES_INT
}

func (b *ServBean) GetAllLinks() *bean.Bean {
	return b.GetBean(consts.LINKS)
}

func (b *ServBean) ContainsItem(itemCode string) bool {
	return b.GetBean(consts.ITEMS).Contains(itemCode)
}

func (b *ServBean) ClearDataCache(dataId string) {
	if b.HasCache() { // 设置了缓存处理
		if len(dataId) > 0 {
			cache.CacheMgr.ClearCache(b.GetSrcId())
		} else {
			cache.CacheMgr.Remove(dataId, b.GetSrcId())
		}
	}
}

func (b *ServBean) HasCache() bool {
	return b.GetInt("SERV_CACHE_FLAG") == consts.YES_INT
}

func (b *ServBean) GetDataCache(id string) *bean.Bean {
	if value := cache.CacheMgr.Get(id, b.GetSrcId()); value != nil {
		return value.(*bean.Bean)
	}
	return nil
}

func (b *ServBean) HasFalseDelete() bool {
	return b.GetInt("SERV_DELETE_FLAG") == consts.YES_INT && b.ContainsItem("S_FLAG")
}

func (b *ServBean) SetDataCache(key string, data *bean.Bean) {
	cache.CacheMgr.Set(key, data, b.GetSrcId())
}

func (b *ServBean) HasLock() bool {
	return b.GetInt("SERV_LOCK_FLAG") == consts.YES_INT
}

func (b *ServBean) GetCombineItems() []*bean.Bean {
	return b.GetBeans(consts.COMBINE_ITEMS)
}

func NewServBean() *ServBean {
	return &ServBean{}
}

func NewServBean2(m map[string]interface{}) *ServBean {
	bean := bean.NewBean2(m)
	return &ServBean{bean}
}
